﻿AddBasicInfo = function()
{
    var count = $id("hf.bicount").innerHTML;
    var load = $id("load_BasicInfo");
    
    StartLoadingAnimation(load);
    
    cbkBasicInfo_c.PerformCallback("add" + DELIMITER + count);
}

BasicInfo_OnClick = function(){
    var load = $id("load_BasicInfo");
    var mode = trim($id("lnkedit_BasicInfo").innerHTML);

    StartLoadingAnimation(load);
    
    if(mode == "save")
    {
        if(confirm("save changes?"))
        {
            // di sini kirim data perubahannya
            var input = getBasicInfoData();
            if(input)
            {
                cbkBasicInfo_c.PerformCallback(mode + DELIMITER + input);
            }
            else
            {
                EndLoadingAnimation(load);
                return;
            }
        }
        else
        {
            EndLoadingAnimation(load);
            return;
        }
    }
    else if(mode == "edit")
    {
        cbkBasicInfo_c.PerformCallback(mode);
    }
}

cbkBasicInfo_c_CallbackComplete = function(e){
    var div = $id("div_BasicInfo");
    var load = $id("load_BasicInfo");
    var lnk = $id("lnkedit_BasicInfo");
    var dscd = $id("lnkdscd_BasicInfo");
    var add = $id("lnkadd_BasicInfo");

    EndLoadingAnimation(load);
    
    var mode = e.parameter.split(DELIMITER)[0];
    
    if(mode == "edit")
    {
        lnk.innerHTML = "save";
        dscd.style.display = "block";
        add.style.display = "block";
        div.innerHTML = e.result;
    }
    else if(mode == "add")
    {
        if( div.insertAdjacentHTML )
        {
            div.insertAdjacentHTML( "beforeEnd", e.result );
        }
        else
        {
            var div2 = document.createElement('div');
            div2.innerHTML = e.result;
            div.appendChild(div2);
        }
        
        var hf = $id("hf.bicount");
        hf.innerHTML = parseInt(hf.innerHTML) + 1; 
    }
    else if(mode == "changelogo")
    {
        var div = $id("popuplayer");
        div.innerHTML = e.result;
    }
    else if(mode == "savelogo")
    {
        var div = $id("popuplayer");
        div.innerHTML = "";
        div.style.display = "none";
    }
    else
    {
        lnk.innerHTML = "edit";
        dscd.style.display = "none";
        add.style.display = "none";
        
        var divSummary = $id("div_Summary");
        
        var resultSplitted = e.result.split(DELIMITER);
        div.innerHTML = resultSplitted[0];
        divSummary.innerHTML = resultSplitted[1];
    }
}
BasicInfo_Discard = function(){
    if(confirm("discard changes?"))
    {
        var load = $id("load_BasicInfo");
        
        StartLoadingAnimation(load);
        
        cbkBasicInfo_c.PerformCallback("discard");
    }
    else
    {
        EndLoadingAnimation(load);
        return;
    }
}

AddNewContact = function()
{
    var mode = $id("lnkadd_ContactInfo").innerHTML;
    var load = $id("load_ContactInfo");
    var contactid = $id("hidContactId");
    
    StartLoadingAnimation(load);

    if(mode == "save")
    {
        var input = getContactInfoData();
        if( input )
        {
            cbkContactInfo_c.PerformCallback(mode + DELIMITER + contactid.value + DELIMITER + input);
        }
        else
        {
            EndLoadingAnimation(load);
        }
    }
    else
    {
        contactid.value = "-1_Y";
        cbkContactInfo_c.PerformCallback("add");
    }
}

AddInfo2Contact = function(elmt)
{
    var count = $id("hf.cicount").innerHTML;
    var load = $id("load_ContactInfo");
    
    StartLoadingAnimation(load);

    cbkContactInfo_c.PerformCallback("addinfo" + DELIMITER + count);
}

ContactInfo_OnClick = function(){
    var load = $id("load_ContactInfo");
    var mode = trim($id("lnkedit_ContactInfo").innerHTML);
    var contactid = $id("hidContactId").value;

    StartLoadingAnimation(load);

    if(mode == "save")
    {
        if(confirm("save changes?"))
        {
            // di sini kirim data perubahannya
            var input = getContactInfoData();
            
            if( input )
            {
                cbkContactInfo_c.PerformCallback(mode + DELIMITER + contactid + DELIMITER + input);
            }
            else
            {
                EndLoadingAnimation(load);
            }
        }
        else
        {
            EndLoadingAnimation(load); 
            return;
        }
    }
    else if(mode == "edit")
    {
        cbkContactInfo_c.PerformCallback(mode + DELIMITER + contactid);
    }
}

cbkContactInfo_c_CallbackComplete = function(e){
    var div = $id("div_ContactInfo");
    var load = $id("load_ContactInfo");
    var lnk = $id("lnkedit_ContactInfo");
    var dscd = $id("lnkdscd_ContactInfo");
    var add = $id("lnkadd_ContactInfo");

    EndLoadingAnimation(load);
    
    var mode = e.parameter.split(DELIMITER)[0];
    
    if(mode == "edit")
    {
        lnk.innerHTML = "save";
        dscd.style.display = "block";
        add.style.display = "none";
        div.innerHTML = e.result;
    }
    else if(mode == "add")
    {
        div.innerHTML = e.result;
        
        add.innerHTML = "save";
        lnk.style.display = "none";
        dscd.style.display = "block";
        div.innerHTML = e.result;
        
    }
    else if(mode == "addinfo")
    {
        var adder = $id("ad.ci");
        if( adder.parentNode.insertAdjacentHTML )
        {
            adder.parentNode.insertAdjacentHTML( "beforeBegin", e.result );
        }
        else
        {
            var parent2 = adder.parentNode.parentNode;
            var div2 = document.createElement('div');
            div2.innerHTML = e.result;
            
            parent2.insertBefore(div2, adder.parentNode);
        }
        
        var count = $id("hf.cicount");
        count.innerHTML = parseInt(count.innerHTML) + 1;        
    }
    else if(mode == "show")
    {
        lnk.innerHTML = "edit";
        lnk.style.display = "block";
        dscd.style.display = "none";
        add.innerHTML = "add new contact++";
        add.style.display = "block";
        div.innerHTML = e.result;
    }
    else
    {
        var sxResultSplitted = e.result.split(DELIMITER);
        lnk.innerHTML = "edit";
        lnk.style.display = "block";
        dscd.style.display = "none";
        add.innerHTML = "add new contact++";
        add.style.display = "block";
        div.innerHTML = sxResultSplitted[1];
        
        var contactid = $id("hidContactId");
        contactid.value = sxResultSplitted[0];
    }
}
ContactInfo_Discard = function(){
    if(confirm("discard changes?"))
    {
        var load = $id("load_ContactInfo");
        
        StartLoadingAnimation(load);
        
        var contactid = $id("hidContactId").value;
        
        cbkContactInfo_c.PerformCallback("discard" + DELIMITER + contactid);
    }
    else
    {
        EndLoadingAnimation(load);
        return;
    }
}
ShowSelectedContactData = function(){
    var companyid = $id("hidCompanyId");
    var contactid = $id("hidContactId");
    var lovh = $id("lovh_cp_" + companyid.value);
    var load = $id("load_ContactInfo");
    
    contactid.value = lovh.value;
    
    StartLoadingAnimation(load);
    
    cbkContactInfo_c.PerformCallback("show" + DELIMITER + contactid.value);    
}
SaveSelectedPositionId = function(){
    var idControl = "CI.JBK";
    var lovh = $id("lovh_" + idControl);
    var hf = $id("hf_" + idControl);
    
    hf.value = lovh.value;
}

BriefDesc_OnClick = function(){
    var load = $id("load_BriefDesc");
    var mode = trim($id("lnkedit_BriefDesc").innerHTML);
    
    StartLoadingAnimation(load);
    
    if(mode == "save")
    {
        if(confirm("save changes?"))
        {
            cbkBriefDesc_c.PerformCallback(mode + DELIMITER + hteBriefDesc_c.GetHtml());
        }
        else
        {
            EndLoadingAnimation(load);
            return;
        }
    }
    else if(mode == "edit")
    {
        cbkBriefDesc_c.PerformCallback(mode);        
    }
}

cbkBriefDesc_c_CallbackComplete = function(e){
    var div = $id("div_BriefDesc");
    var load = $id("load_BriefDesc");
    var dscd = $id("lnkdscd_BriefDesc");
    var lnk = $id("lnkedit_BriefDesc");
    var cnt = $id("cnt_BriefDesc");
    var edt = $id("edt_BriefDesc");

    EndLoadingAnimation(load);

    var content = e.result;
    
    if(e.parameter == "edit")
    {
        lnk.innerHTML = "save";
        dscd.style.display = "block";
        cnt.style.display = "none";
        edt.style.height = "auto";
        edt.style.width = "auto";
        edt.style.overflow = "";
        hteBriefDesc_c.SetHtml(content)
    }
    else
    {
        lnk.innerHTML = "edit";
        dscd.style.display = "none";
        cnt.style.display = "block";
        edt.style.height = "0px";
        edt.style.width = "0px";
        edt.style.overflow = "hidden";
        cnt.innerHTML = content;
    }
}

BriefDesc_Discard = function(){
    if(confirm("discard changes?"))
    {
        var mode = "discard";
        var load = $id("load_BriefDesc");
        
        StartLoadingAnimation(load);
        
        cbkBriefDesc_c.PerformCallback(mode);
    }
    else
    {
        EndLoadingAnimation(load);
        return;
    }
}


toggle = function (fsid)
{
    var div = $id("div_" + fsid);
    var ld = $id("lnkdscd_" + fsid);
    var le = $id("lnkedit_" + fsid);
    
    var display = "";
    
    if( div.style.display != "none" )
    {
        display = "none";
    }
    
    div.style.display = display;
    
    if( le != null )
    {
        le.style.display = display;
    }
    
    if( ld == null )
    {
        return;
    }
    
    if( trim(le.innerHTML) == "edit" )
    {
        ld.style.display = "none";
    }
    else
    {
        ld.style.display = display;
    }
}

getBasicInfoData = function()
{
    //var inputs = $nm("txt");
    var divBI = $id("div_BasicInfo");
    var inputs = $nm("txt", divBI, "input");
    var output = "";
    var hashArray = new Array();
    var iDuplicateCount = 0;
    var iNotUploadedCount = 0;
    var iEmptyTypeCount = 0;
    var iEmptyValueCount = 0;
    var iInvalidCount = 0;
    
    for( var i=0; i < inputs.length; i++ )
    {
        if( i > 0 )
        {
            output += DELIMITER;
        }
        
        var item = inputs[i];
        var lov = $id("lov_" + item.id);
        
        if( lov == null )
        {
            output += item.id + NEWLINE + item.value;
        }
        else
        {
            var lovh = $id("lovh_" + item.id);
            lov.style.backgroundColor = item.style.backgroundColor = "#FFFFFF";
            
            if(hashArray[lovh.value] != null)
            {
                lov.style.backgroundColor = "#FFAAAA";
                iDuplicateCount++;
            }
            
            var uplbtn = $id("uplbtn_" + item.id);
            
            if( uplbtn != null && item.value != "" )
            {
                item.style.backgroundColor = "#FFAAAA";
                iNotUploadedCount++;
            }
            
            if( lovh.value != "" && item.value != "" )
            {
                hashArray[lovh.value] = item.value;
                output += lov.value + NEWLINE + lovh.value + NEWLINE + item.value;        
            }
            else if ( lovh.value != "" )
            {
                iEmptyValueCount++;
                item.style.backgroundColor = "#FFAAAA";
            }
            else if ( item.value != "" )
            {
                iEmptyTypeCount++;
                lov.style.backgroundColor = "#FFAAAA";
            }
            
            if( lov.style.backgroundColor == "#FFAAAA" || item.style.backgroundColor == "#FFAAAA" )
            {
                iInvalidCount++;
            }
            
        }
    }
    
    var sxError = "";
    
    if( iDuplicateCount > 0 )
    {
        sxError += "terdapat jenis info yang sama! ";
    }
    
    if( iNotUploadedCount > 0 )
    {
        sxError += "terdapat file yang belum diupload! ";
    }
    
    if( iEmptyTypeCount > 0 )
    {
        sxError += "terdapat field jenis info yang belum diisi! ";
    }
    
    if( iEmptyValueCount > 0 )
    {
        sxError += "terdapat field isian info yang belum diisi! ";
    }
    
    if( iInvalidCount > 0 )
    {
        sxError += "terdapat pengisian info yang tidak sesuai! ";
    }
    
    if( sxError != "" )
    {
        alert(sxError);
        return false;
    }
    
    return output;
}

getContactInfoData = function()
{
    var divCI = $id("div_ContactInfo");
    var inputs = $nm("txt", divCI, "input");
    var select = $nm("txt", divCI, "select");
    //var inputs = $nm("txtci");
    var output = "";
    var hashArray = new Array();
    var iDuplicateCount = 0;
    var iNotUploadedCount = 0;
    var iEmptyTypeCount = 0;
    var iEmptyValueCount = 0;
    var iInvalidCount = 0;
    var iMandatoryCount = 0;
    var isCPT = $id("CI.CPT");
	
    for( var i=0; i < select.length; i++ )
    {
        var iNext = inputs.length + i;
        inputs[iNext] = select[i];
    }

    for( var i=0; i < inputs.length; i++ )
    {
        if( i > 0 )
        {
            output += DELIMITER;
        }
        
        var item = inputs[i];
        var lov = $id("lov_" + item.id);
        var mandat = $id("mandat_" + item.id);
        
        if( lov == null )
        {
            if( item.id == "CI.CPT" )
            {
                output += item.id + NEWLINE + (item.checked ? "Y" : "N");
                continue;
            }
            output += item.id + NEWLINE + item.value;
            if((mandat != null && mandat.value.toUpperCase() == 'Y' && item.value == "" ) || (item.id.toUpperCase() == "HF_CI.JBK" && (item.value == "" || parseInt(item.value) < 0) ))
            {
                if(!isCPT.checked)// && (item.id == "CI.TPK" || item.id == "CI.HPK" || item.id == "CI.EMK")) 
                {
                    //do nothing
                }
                else
                {
                    iMandatoryCount++;
                    item.style.backgroundColor = "#FFAAAA";
                }
            }
        }
        else
        {
            var lovh = $id("lovh_" + item.id);
            lov.style.backgroundColor = item.style.backgroundColor = "#FFFFFF";
            
            if(hashArray[lovh.value] != null)
            {
                lov.style.backgroundColor = "#FFAAAA";
                iDuplicateCount++;
            }
            
            var uplbtn = $id("uplbtn_" + item.id);
            
            if( uplbtn != null && item.value != "" )
            {
                item.style.backgroundColor = "#FFAAAA";
                iNotUploadedCount++;
            }
            
            if( mandat != null && mandat.value.toUpperCase() == 'Y' && item.value == "" )
            {
                iMandatoryCount++;
                item.style.backgroundColor = "#FFFFFF";
            }
            
            if( lovh.value != "" && item.value != "" )
            {
                hashArray[lovh.value] = item.value;
                output += lov.value + NEWLINE + lovh.value + NEWLINE + item.value;        
            }
            else if ( lovh.value != "" )
            {
                iEmptyValueCount++;
                item.style.backgroundColor = "#FFAAAA";
            }
            else if ( item.value != "" )
            {
                iEmptyTypeCount++;
                lov.style.backgroundColor = "#FFAAAA";
            }
            
            if( lov.style.backgroundColor == "#FFAAAA" || item.style.backgroundColor == "#FFAAAA" )
            {
                iInvalidCount++;
            }
            
        }
    }
    
    var sxError = "";
    
    if( iDuplicateCount > 0 )
    {
        sxError += "terdapat jenis info yang sama! ";
    }
    
    if( iNotUploadedCount > 0 )
    {
        sxError += "terdapat file yang belum diupload! ";
    }
    
    if( iEmptyTypeCount > 0 )
    {
        sxError += "terdapat field jenis info yang belum diisi! ";
    }
    
    if( iEmptyValueCount > 0 )
    {
        sxError += "terdapat field isian info yang belum diisi! ";
    }
    
    if( iInvalidCount > 0 )
    {
        sxError += "terdapat pengisian info yang tidak sesuai! ";
    }
    
    if( iMandatoryCount > 0 )
    {
        sxError += "terdapat field wajib yang belum diisi! ";
    }
    
    if( sxError != "" )
    {
        alert(sxError);
        return false;
    }
    
    return output;
}

StartLoadingAnimation = function(div){
    div.innerHTML = "<img src='" + LOADING.src + "' alt='' />";    
}

EndLoadingAnimation = function(div){
    div.innerHTML = "";    
}


changeCompanyLogo = function()
{
    var div = $id("popuplayer");
    div.style.display = "block";
    div.innerHTML = "<img src='" + LOADING.src + "' alt='' />";
    renderFirst();
    
    cbkBasicInfo_c.PerformCallback("changelogo");
}

closeEditor = function(){
    window.location.reload(true);
}